package cn.zjnu.study.service;

import cn.zjnu.study.bean.GlobalProperties;
import cn.zjnu.study.bean.User;
import cn.zjnu.study.bean.Video;
import cn.zjnu.study.bean.VideoExample;
import cn.zjnu.study.dao.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.List;

@Service
public class VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private GlobalProperties globalProperties;

    public List<Video> getVideosByUserId(int id) {
        VideoExample example = new VideoExample();
        VideoExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<Video> videos = videoMapper.selectByExample(example);
        return videos;
    }

    public Video getVideoById(int id) {
        VideoExample example = new VideoExample();
        VideoExample.Criteria criteria = example.createCriteria();
        criteria.andVideoIdEqualTo(id);
        List<Video> videos = videoMapper.selectByExample(example);
        if (videos.size() == 0) {
            return null;
        }
        return videos.get(0);
    }

    public List<Video> getAllVideos() {
        return videoMapper.selectByExample(null);
    }

    public List<Video> searchByName(String videoName) {
        VideoExample example = new VideoExample();
        VideoExample.Criteria criteria = example.createCriteria();
        String likeName = "%" + videoName + "%";
        criteria.andVideoNameLike(likeName);
        return videoMapper.selectByExample(example);
    }

    public String uploadVideo(MultipartFile file, User user, MultipartFile cover, String videoName) throws IOException {
        String path = "/" + user.getUserId() + "/";
        String videoFileName = file.getOriginalFilename();
        String coverFileName = videoFileName.split("\\.")[0] + ".png";
        String realPath = globalProperties.getBasePath() + path;
        String videoPath = realPath + videoFileName;
        InputStream is = file.getInputStream();
        File outFile = new File(videoPath);
        outFile.getParentFile().mkdirs();
        Files.copy(is, outFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        if (cover != null) {
            Files.copy(cover.getInputStream(),
                    new File(realPath + coverFileName).toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
        Video video = new Video();
        video.setUserId(user.getUserId());
        video.setVideoName(videoName);
        video.setUploadTime(new Date());
        video.setTypeId(1);
        video.setPath(path + videoFileName);
        video.setCover(cover == null ? null : path + coverFileName);
        videoMapper.insert(video);
        return path;
    }

    public void insertVideo(Video video) {
        videoMapper.insert(video);
    }
}
